<template>
  <div class="body-container layout-padding">
    <el-form ref="pwdForm" size="default" :model="entity" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="old password" prop="oldpwd">
        <el-input v-model="entity.oldpwd" show-password placeholder="old password" ref="oldpwd" />
      </el-form-item>
      <el-form-item label="new password" prop="newpwd">
        <el-input v-model="entity.newpwd" show-password placeholder="new password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit" @click="btnSubmit">ok</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="changePwd">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";
import mittBus from "/@/utils/mitt";

// 定义变量内容
const pwdForm = ref(null);
const oldpwd = ref(null);

let entity = ref({
  oldpwd: '',
  newpwd : '',
})
const rules = reactive({
  oldpwd: [
    { required: true, message: 'please input old password', trigger: 'blur' },
    { min: 8, max: 20, message: 'Length between 8 and 20 characters', trigger: 'blur' }
  ],
  newpwd: [
    { required: true, message: 'please input new password', trigger: 'blur' },
    { min: 8, max: 20, message: 'Length between 8 and 20 characters', trigger: 'blur' }
  ],
})

const btnSubmit = () =>{
  if(!pwdForm.value) return;
  pwdForm.value.validate((valid)=>{
    if(valid){
      request({
        url: '/sys/useradmin/changepwd',
        data: entity.value
      }).then( res =>{
        if(res.code === "200"){
          ElMessage.success(res.msg)
        }else{
          ElMessage.error(res.msg);
        }
      })
    }else{
      return false;
    }
  })
}

onMounted(() => {
  nextTick(()=>{
    oldpwd.value.focus()
  })
});
</script>

<style scoped>

</style>
